package com.tfjybj.itoo.exam.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.StudentScoreEntity;
import com.tfjybj.itoo.exam.model.StudentScoreModel;
import com.tfjybj.itoo.exam.provider.service.StudentScoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * StudentScoreController
 * studentScore表
 *
 * @author 任晓帅
 * @version 2.0.0
 * @since 2.0.0 2018-11-15 19:40:28
 */
@Api(tags = {"studentScore表接口"})
@RequestMapping(value = "/studentScore")
@RestController
public class StudentScoreController {

    @Resource
    private StudentScoreService studentScoreService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model StudentScoreModel
     * @return 添加的结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody StudentScoreModel model) {
		if (StringUtils.isEmpty(model.getStudentCode())){
            return ItooResult.build(ItooResult.FAIL, "studentCode为空");
        }
		if (StringUtils.isEmpty(model.getStudentName())){
            return ItooResult.build(ItooResult.FAIL, "studentName为空");
        }
		if (StringUtils.isEmpty(model.getExamineeId())){
            return ItooResult.build(ItooResult.FAIL, "examineeId为空");
        }
		if (StringUtils.isEmpty(model.getExaminationId())){
            return ItooResult.build(ItooResult.FAIL, "examinationId为空");
        }
		if (StringUtils.isEmpty(model.getExamClassifyId())){
            return ItooResult.build(ItooResult.FAIL, "examClassifyId为空");
        }
		if (StringUtils.isEmpty(model.getTemplateId())){
            return ItooResult.build(ItooResult.FAIL, "templateId为空");
        }
		if (StringUtils.isEmpty(model.getVersion())){
            return ItooResult.build(ItooResult.FAIL, "version为空");
        }
        StudentScoreEntity studentScoreEntity = new StudentScoreEntity();
        BeanUtils.copyProperties(model, studentScoreEntity);
        studentScoreService.save(studentScoreEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功          
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        studentScoreService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        studentScoreService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model StudentScoreModel
     * @return 修改后的结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id修改studentScore")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody StudentScoreModel model) {
		if (StringUtils.isEmpty(model.getStudentCode())){
            return ItooResult.build(ItooResult.FAIL, "studentCode为空");
        }
		if (StringUtils.isEmpty(model.getStudentName())){
            return ItooResult.build(ItooResult.FAIL, "studentName为空");
        }
		if (StringUtils.isEmpty(model.getExamineeId())){
            return ItooResult.build(ItooResult.FAIL, "examineeId为空");
        }
		if (StringUtils.isEmpty(model.getExaminationId())){
            return ItooResult.build(ItooResult.FAIL, "examinationId为空");
        }
		if (StringUtils.isEmpty(model.getExamClassifyId())){
            return ItooResult.build(ItooResult.FAIL, "examClassifyId为空");
        }
		if (StringUtils.isEmpty(model.getTemplateId())){
            return ItooResult.build(ItooResult.FAIL, "templateId为空");
        }
		if (StringUtils.isEmpty(model.getVersion())){
            return ItooResult.build(ItooResult.FAIL, "version为空");
        }
        StudentScoreEntity studentScoreEntity = new StudentScoreEntity();
        BeanUtils.copyProperties(model, studentScoreEntity);
        studentScoreService.updateById(studentScoreEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找StudentScore
     *
     * @param id 主键id
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        StudentScoreEntity studentScoreEntity = studentScoreService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", studentScoreEntity);
    }

    /**
     * 分页查询所有StudentScore
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "分页查询所有StudentScore")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
								   @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize) {
        PageInfo<StudentScoreEntity> studentScores = studentScoreService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", studentScores);
    }
	
	/**
     * 根据学号模糊查询studentScore
     *
     * @param studentCode  学号
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的studentScore
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据学号模糊查询studentScore", notes = "分页根据学号模糊查询studentScore")
    @GetMapping(value = "queryStudentScoreByLikeStudentCode/{pageNo}/{pageSize}")
    public ItooResult queryStudentScoreByLikeStudentCode(@RequestParam(required = false, defaultValue = "") String studentCode,
                                                         @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                                         @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<StudentScoreEntity> studentScoreList = studentScoreService.queryByLikeStudentCode(studentCode, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", studentScoreList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 根据试卷id和学生id更新学生总分数
     *
     * @return 返回修改受影响行数
     * @author maying
     * @since 2018-11-26 19:54:21
     */
    @ApiOperation(value = "根据考试id和学生id更新学生总分数，分数传给")
    @PostMapping(value = "/upStudentScore")
    public ItooResult upStudentScore(@RequestBody StudentScoreModel stuScore) {
        if (stuScore == null) {
            return ItooResult.build(ItooResult.FAIL, "参数传值为空！");
        }
        try {

            return ItooResult.build(ItooResult.SUCCESS, "更新成功！", studentScoreService.upStudentScore(stuScore));
        } catch (Exception e) {
            return ItooResult.build(ItooResult.FAIL, "更新失败！");
        }
    }

    /**
     * 查询低分数据
     *
     * @return 低分班级
     * @author 樊建华
     * @since 2019年1月22日14:36:11
     */

    @ApiOperation(value = "低分班级")
    @GetMapping(value = "/selectlowclass/{number}/{operator}")
    public ItooResult selectlowclass (@ApiParam(name = "number",value = "预警分数",required = true,example = "0")@PathVariable String number,
                                      @ApiParam(name = "operator",value = "操作人",required = true)@PathVariable String operator){
        List<StudentScoreModel> studentScoreModelList = studentScoreService.selectlowclass(number,operator);
        if (studentScoreModelList == null)
            return ItooResult.build(ItooResult.SUCCESS,"没有低于此分数的学生");
        else  return ItooResult.build(ItooResult.SUCCESS, "查询成功", studentScoreModelList);
    }

    /**
     * 查询低分学生
     *
     * @return 低分学生
     * @author 樊建华
     * @since 2019年1月22日14:36:11
     */
    @ApiOperation(value = "低分学生")
    @GetMapping(value = "/selectlowstudent/{number}/{templateId}/{classId}/{operator}")
    public ItooResult selectlowstudent (@ApiParam(name = "number",value = "预警分数",required = true,example = "0")@PathVariable String number,
                                      @ApiParam(name = "templateId",value = "模板id",required = true)@PathVariable String templateId,
                                      @ApiParam(name = "classId",value = "班级id",required = true)@PathVariable String classId,
                                        @ApiParam(name = "operator",value = "操作人",required = true)@PathVariable String operator){

            List<StudentScoreModel> studentScoreModelList = studentScoreService.selectlowstudent(number,templateId,classId,operator);
            if (studentScoreModelList == null)
                return ItooResult.build(ItooResult.SUCCESS,"没有低于此分数的学生");
            else  return ItooResult.build(ItooResult.SUCCESS, "查询成功", studentScoreModelList);

    }




}

